package org.jfk.sys.security;

import java.util.List;

import org.apache.log4j.Logger;
import org.jfk.core.utils.SqlFileUtils;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.Table;
import com.jfinal.plugin.activerecord.TableMapping;

public class Urls  extends Model<Urls>{
	private static Logger logger = Logger.getLogger(Urls.class);	
	public static final Urls dao;
	public static final Table table;
	public static final String tableName;
	static {
		dao = new Urls();
		table = TableMapping.me().getTable(Urls.class);
		tableName = table.getName();
	}
	
	public Urls findByUrl(String url){
		//return Urls.dao.findFirst("select * from "+"jfk_v_menu_urls"+" where url = ?",url);
		return Urls.dao.findFirst("select * from "+"jfk_menu"+" where url like concat(?,'%')",url);
	}

	/**
	 * 获取所有权限资源列表
	 * @return
	 */
	public List<Record> getAllUrls(){
		String sql = SqlFileUtils.readSqlFromFile("sys.getAllUrls.sql");
		List<Record> urls = Db.find(sql);
		return urls;
	}

	/**
	 * 获取指定用户已授权的权限资源列表
	 * @return
	 */
	public List<Record> getRightUrlsByUser(int userId){
		String sql = SqlFileUtils.readSqlFromFile("sys.getRightUrlsByUser.sql");
		List<Record> urls = Db.find(sql,userId);
		return urls;
	}
	
	/**
	 * 获取指定角色已授权的权限资源列表
	 * @return
	 */
	public List<Record> getRightUrlsByRole(int roleId){
		String sql = SqlFileUtils.readSqlFromFile("sys.getRightUrlsByRole.sql");
		List<Record> urls = Db.find(sql,roleId);
		return urls;
	}	
	
}
